package com.stw.aop;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class ControllerAop {
    //aop切点
    @Pointcut("execution(* com.stw.controller.*.*(..))")
    public void addLog(){};

    //在方法前
    @Before("addLog()")
    public void before() {
        System.out.println("好好學習");
    }

    //在方法周围
    @Around("addLog()")
    public Object  testAround(ProceedingJoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        //查看入参
        for (Object arg : args) {
            System.out.println(arg);
        }
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        String[] parameterNames = methodSignature.getParameterNames();

        //打印方法名
        System.out.println(methodSignature.getMethod().getName());
        System.out.println(methodSignature.getName());

        //打印参数名
        for (String parameterName : parameterNames) {
            System.out.println(parameterName);
        }

        //执行的对象
        Object target = joinPoint.getTarget();
        System.out.println(target);
        try {
            //查看执行方法时间
            long l = System.currentTimeMillis();
            Object proceed = joinPoint.proceed();
            long l1 = System.currentTimeMillis();
            System.out.println(l1-l);

            //返回方法，若不返回，就没有结果
            return   proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return  null;
    }
}
